//
//  CyclicCardFlowLayout.swift
//  CyclicCard
//
//  Created by Tony on 17/1/11.
//  Copyright © 2017年 Tony. All rights reserved.
//

import UIKit

class CyclicCardFlowLayout: UICollectionViewFlowLayout {
    
    var lastProposedContentOffset:CGFloat = 0.0
    
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        // ---------案例1：图片很顺畅地滑动，直到静止
//        let targetRect = CGRect(x: proposedContentOffset.x, y: 0.0, width:  self.collectionView!.bounds.size.width, height: self.collectionView!.bounds.size.height)
//
//        // 目标区域中包含的cell
//        let attriArray = super.layoutAttributesForElements(in: targetRect)! as [UICollectionViewLayoutAttributes]
//        // collectionView落在屏幕中点的x坐标
//        let horizontalCenterX = proposedContentOffset.x + (self.collectionView!.bounds.width / 2.0)
//        var offsetAdjustment = CGFloat(MAXFLOAT)
//        for layoutAttributes in attriArray {
//            let itemHorizontalCenterX = layoutAttributes.center.x
//            // 找出离中心点最近的
//            if(abs(itemHorizontalCenterX-horizontalCenterX) < abs(offsetAdjustment)) {
//                offsetAdjustment = itemHorizontalCenterX-horizontalCenterX
//            }
//        }
//
//        //补回offSet，让item居中显示
//        let proposedX = proposedContentOffset.x + offsetAdjustment
//        print("offsetAdjustment:\(offsetAdjustment)")
//
//        //返回collectionView最终停留的位置
//        return CGPoint(x: proposedX, y: proposedContentOffset.y)



        //----------案例2：图片分页（在上述基础上改一改）
        let targetRect = CGRect(x: collectionView!.contentOffset.x, y: 0.0, width:  self.collectionView!.bounds.size.width, height: self.collectionView!.bounds.size.height)

        // 目标区域中包含的cell
        let attriArray = super.layoutAttributesForElements(in: targetRect)! as [UICollectionViewLayoutAttributes]
        // collectionView落在屏幕中点的x坐标
        let horizontalCenterX = collectionView!.contentOffset.x + (self.collectionView!.bounds.width / 2.0)
        var offsetAdjustment = CGFloat(MAXFLOAT)
        for layoutAttributes in attriArray {
            let itemHorizontalCenterX = layoutAttributes.center.x
            // 找出离中心点最近的
            if(abs(itemHorizontalCenterX-horizontalCenterX) < abs(offsetAdjustment)) {
                offsetAdjustment = itemHorizontalCenterX-horizontalCenterX
            }
        }

        //补回offSet，让item居中显示
        var proposedX = collectionView!.contentOffset.x + offsetAdjustment
        let pageWidth = itemSize.width + minimumLineSpacing

        /*
         velocity.x = 0，表示手指没有向左也没有向右，垂直离开屏幕
         velocity.x < 0，表示手指向右滑动，正常offsetAdjustment>0
         velocity.x > 0，表示手指向左滑动，正常offsetAdjustment<0
         */
        if velocity.x < 0 { //向右滑动
            if offsetAdjustment > 0 {
                proposedX = proposedX - pageWidth
            }
        }else if velocity.x > 0 { //向左滑动
            if offsetAdjustment < 0 {
                proposedX = proposedX + pageWidth
            }
        }
        print("velocity:\(velocity), offsetAdjustment:\(offsetAdjustment)");

        //返回collectionView最终停留的位置
        return CGPoint(x: proposedX, y: collectionView!.contentOffset.y)
    }
    
    let ActiveDistance : CGFloat = 400 //垂直缩放除以系数
    let ScaleFactor : CGFloat = 0.25     //缩放系数  越大缩放越大
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        let array = super.layoutAttributesForElements(in: rect)
        var visibleRect = CGRect()
        visibleRect.origin = self.collectionView!.contentOffset
        visibleRect.size = self.collectionView!.bounds.size
        
        for attributes in array! {
            let distance = visibleRect.midX - attributes.center.x
            let normalizedDistance = abs(distance / ActiveDistance)
            let zoom = 1 - ScaleFactor * normalizedDistance
            attributes.transform3D = CATransform3DMakeScale(1.0, zoom, 1.0)
//            let alpha = 1 - normalizedDistance
//            attributes.alpha = alpha
        }
        return array
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        //滑动放大缩小  需要实时刷新layout
        return true
    }
}
